package com.zengyu.toiusuapplication.navication;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupMenu;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;

import com.zengyu.toiusuapplication.MainActivity;
import com.zengyu.toiusuapplication.R;
import com.zengyu.toiusuapplication.change.upLoadComplaintURL;
import com.zengyu.toiusuapplication.order.OrderListActivity;

//首页页面
public class HomeFragment extends Fragment {

    private LinearLayout addAllComplaintLayout,ic_userManager,ic_joyManager;
    private LinearLayout myOrderLayout,ic_dicManager,ic_menuManager;
    private ImageView ivNavigation;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);

        addAllComplaintLayout = view.findViewById(R.id.addAllComplaint);
        myOrderLayout = view.findViewById(R.id.myOrder);
        ic_userManager = view.findViewById(R.id.ic_userManager);
        ic_joyManager = view.findViewById(R.id.ic_joyManager);
        ic_dicManager = view.findViewById(R.id.ic_dicManager);
        ic_menuManager = view.findViewById(R.id.ic_menuManager);
        ivNavigation = view.findViewById(R.id.ivNavigation);
        ImageView noticeIcon = view.findViewById(R.id.ivNotice);
        noticeIcon.setOnClickListener(v -> showNotificationDialog());
        ImageView menuIcon = view.findViewById(R.id.ivMenu);

        menuIcon.setOnClickListener(v -> {
            PopupMenu popup = new PopupMenu(requireContext(), v);
            popup.getMenuInflater().inflate(R.menu.menu_main, popup.getMenu());

            popup.setOnMenuItemClickListener(item -> {
                int id = item.getItemId();
                if (id == R.id.menu_profile) {
                    Toast.makeText(getActivity(), "还在开发中 " , Toast.LENGTH_SHORT).show();
                    //startActivity(new Intent(requireContext(), ProfileActivity.class));
                    return true;
                } else if (id == R.id.menu_settings) {
                    //startActivity(new Intent(requireContext(), SettingsActivity.class));
                    Toast.makeText(getActivity(), "你还没有相关权限 " , Toast.LENGTH_SHORT).show();
                    return true;
                }
                return false;
            });

            popup.show();
        });

        if (addAllComplaintLayout != null) {
            addAllComplaintLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    navigateToComplaint5gActivity();
                }
            });
        }

        if (myOrderLayout != null) {
            myOrderLayout.setOnClickListener(v -> {
                navigateToOrderListActivity();
            });
        }

        if (ic_userManager != null) {
            ic_userManager.setOnClickListener(v -> {
                Toast.makeText(getActivity(), "你没有相关权限" , Toast.LENGTH_SHORT).show();
            });
        }

        if (ic_joyManager != null) {
            ic_joyManager.setOnClickListener(v -> {
                Toast.makeText(getActivity(), "你没有相关权限" , Toast.LENGTH_SHORT).show();
            });
        }

        if (ic_dicManager != null) {
            ic_dicManager.setOnClickListener(v -> {
                Toast.makeText(getActivity(), "你没有相关权限 " , Toast.LENGTH_SHORT).show();
            });
        }

        if (ic_menuManager != null) {
            ic_menuManager.setOnClickListener(v -> {
                Toast.makeText(getActivity(), "你没有相关权限 " , Toast.LENGTH_SHORT).show();
            });
        }

        if (ivNavigation != null) {
            ivNavigation.setOnClickListener(v -> {
                // 通知 Activity 打开抽屉
                if (getActivity() instanceof MainActivity) {
                    ((MainActivity) getActivity()).openDrawer();
                }
            });
        }

        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    private void navigateToComplaint5gActivity() {
        Intent intent = new Intent(getActivity(), upLoadComplaintURL.class);
        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
    }

    private void navigateToOrderListActivity() {
        Intent intent = new Intent(getActivity(), OrderListActivity.class);
        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
    }

    private void showNotificationDialog() {
        new AlertDialog.Builder(requireContext())
                .setTitle("通知")
                .setMessage("您有新的系统消息：\n\n- 系统将于今晚进行维护。\n- 请保存好您的工作进度。")
                .setPositiveButton("确定", null)
                .show();
    }

}

